package com.bibrApp.difu.fragment.account_mvp;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bibrApp.difu.R;
import com.bibrApp.difu.proto2javabean.WalletAssetBean;
import com.bibrApp.difu.utils.BigdecimalUtils;
import com.bibrApp.difu.utils.CalendarUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.satoshilabs.trezor.lib.protobuf.TrezorMessageManagement;

import org.web3j.utils.Convert;

import java.util.ArrayList;
import java.util.List;

public class AccountAdapter extends BaseQuickAdapter<WalletAssetBean.Transactions, BaseViewHolder> {
    private String coinType;
    private List<WalletAssetBean.Transactions> transactionsList;
    private final static int num2Coin = 1_0000_0000;
    public AccountAdapter() {
        super(R.layout.item_account, new ArrayList<>());
    }

    public void setCoinType(String coinType) {
        this.coinType = coinType;
    }
    public void setTransactionsList(List<WalletAssetBean.Transactions> transactionsList) {
        this.transactionsList = transactionsList;
        notifyDataSetChanged();
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, WalletAssetBean.Transactions item) {
        int position = helper.getAdapterPosition();
        helper.setText(R.id.item_time, CalendarUtils.formatString2Minute(item.getBlockTime()));
        if(mData.size()>1){
            if(position == mData.size() -1){//最后一条
                if(coinType.equalsIgnoreCase("eth")){
                    helper.setText(R.id.item_change,BigdecimalUtils.getBigDecimalPointNum(Convert.fromWei(item.getValue(),Convert.Unit.ETHER),8)+coinType);
                }else{
                    helper.setText(R.id.item_change, BigdecimalUtils.format2Str(Double.parseDouble(item.getValue())/num2Coin,8)+coinType);
                }
            }else{
                if(coinType.equalsIgnoreCase("eth")){
                    helper.setText(R.id.item_change, BigdecimalUtils.getBigDecimalPointNum(Convert.fromWei(mData.get(position+1).getValue(),Convert.Unit.ETHER).subtract(Convert.fromWei(item.getValue(),Convert.Unit.ETHER)),8)+coinType);
                }else{
                    helper.setText(R.id.item_change, BigdecimalUtils.format2Str((Double.parseDouble(mData.get(position+1).getValue())- Double.parseDouble(item.getValue()))/num2Coin,8)+coinType);
                }
            }
        }else{
            helper.setText(R.id.item_change, BigdecimalUtils.format2Str(Double.parseDouble(item.getValue())/num2Coin,8)+coinType);
        }

        helper.setText(R.id.item_sure_num,item.getConfirmations());
    }
}
